<?php
	class Users extends CI_Controller {
	   
       function __construct() {
	       parent::__construct();
           $user = $this->session->userdata('user');
           if (!$user) redirect('admin/login');
           $this->load->model('admin/Dataset_db');
           $this->load->model('users/users_db','Users');
	   }
       
       function adduser() {
            $data['ptitle'] = "Add User";
            if($_POST['btnAddUser']) {
                //print_object($_POST); exit;
                $pdata = array();
                $fields = array(
						"name"
                        ,"roleid"
						,"username"
						,"disabled"
                        ,"mobile"
                        ,"email"
                        ,"address"				
						);
                foreach ($fields as $field) {
    				if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field);
    			}
                
                
                $this->form_validation->set_rules('name', 'Name', 'trim|required|min_length[3]|max_length[100]|xss_clean');
                $this->form_validation->set_rules('roleid', 'Account Type', 'trim|required|integer|xss_clean');
                $this->form_validation->set_rules('username', 'User Name', 'trim|required|min_length[3]|max_length[20]|xss_clean');
                $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|max_length[20]|matches[cpassword]');
                $this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|min_length[6]|max_length[20]');
                $this->form_validation->set_rules('mobile', 'Mobile', 'trim|required|min_length[5]|max_length[15]|xss_clean');
                $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|min_length[5]|max_length[50]|xss_clean');
                $this->form_validation->set_rules('address', 'Address', 'trim|max_length[200]|xss_clean');
                $this->form_validation->run();
                $data["errors"] = $this->form_validation->error_array();
                //print_object($_POST); exit;
                
                if(!$this->Users->is_username_unique($this->input->post("username",true))) {
                    if(is_array($data['errors']))
                        array_push($data["errors"],"Username already taken. Choose a different one.");
                    else
                        $data["errors"] = array("Username already taken. Choose a different one.");
                }
                
                $data["postdata"] = $pdata;
                   
                if (count($data["errors"])>0) {
                    
                } else {
                    $pdata["password"] = md5($this->input->post("password",true));
                    $user = $this->session->userdata('user');
                    $pdata["createdby"] = $user['userid'];
                    $pdata['createdon'] = unix_to_human(now(), true);
                    //print_object($pdata); exit;
                    
                    $save_result = $this->Users->save_user($pdata);
                    if($save_result['result'] == 'S') {
                        $data["successes"] = array('User added successfully.');
                        $data["postdata"] = "";
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
            }
                        
            $navResult = $this->Dataset_db->buildNav();
            $data['rolelist'] = $this->Users->get_roles();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $data['content'] = $this->load->view('admin/adduser',$data,true);
            $this->load->vars($data);
            $this->load->view('admin/default_view'); 
       }
       
       function viewusers() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            
            $data['ptitle'] = "View User";
            //print_object($_POST);exit;
            if( isset( $_POST["userid"] ) ) {
                $mi_result = $this->Users->get_user_list(0,0,array('userid'=> $this->input->post("userid",true)));
                //print_object($mi_result);exit;
                if(count($mi_result) == 0) {
                    $data['errors'] = "Could not find the selected User";
                } else {
                    $data['userid'] = $mi_result[0]['userid'];
                    $data['username'] = $mi_result[0]['username'];
                    $data['roletitle'] = $mi_result[0]['roletitle'];
                    $data['disabled'] = $mi_result[0]['disabled'];
                    $data['name'] = $mi_result[0]['name'];
                    $data['mobile'] = $mi_result[0]['mobile'];
                    $data['email'] = $mi_result[0]['email'];
                    $data['address'] = $mi_result[0]['address'];
                    //$data['firstlogin'] = $mi_result[0]['firstlogin'];
                    $data['createdby'] = $mi_result[0]['createdby'];
                    $data['createdon'] = $mi_result[0]['createdon'];
                    $data['editaction'] = 'admin/users/edituser';
                }
            } else {
                $data['dgdata'] = $this->Users->build_users_dg_data();    
            }
            //print_object($data);exit;
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $data['content'] = $this->load->view('viewuser',$data,true);
            $this->load->vars($data);
            $this->load->view('admin/default_view');
       }
       
       function edituser() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            if(!isset($_POST['userid'])) redirect(site_url('admin/users/viewusers'));
            $data['ptitle'] = "Edit User";
            $data['userid'] = $this->input->post("userid",true);
            $isupdated = false;
            $ispostdata = false;
            //print_object($_POST);exit;
            if( isset( $_POST["btnUpdateUser"] ) ) {
                //print_object($_POST);exit;
                $ispostdata = true;
                $pdata = array();
                $fields = array(
						"name"
                        ,"roleid"
						,"disabled"
                        ,"mobile"
                        ,"email"
                        ,"address"				
						);
                foreach ($fields as $field) {
    				if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field);
    			}

                $data["postdata"] = $pdata;
                
                $this->form_validation->set_rules('userid', 'User ID', 'trim|required|xss_clean');
                $this->form_validation->set_rules('name', 'Name', 'trim|required|min_length[3]|max_length[100]|xss_clean');
                $this->form_validation->set_rules('roleid', 'Account Type', 'trim|required|integer|xss_clean');
                $this->form_validation->set_rules('mobile', 'Mobile', 'trim|required|min_length[5]|max_length[15]|xss_clean');
                $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|min_length[5]|max_length[50]|xss_clean');
                $this->form_validation->set_rules('address', 'Address', 'trim|max_length[200]|xss_clean');
                $this->form_validation->run();
                $data["errors"] = $this->form_validation->error_array();
                //print_object($_POST); exit;
                   
                if (count($data["errors"])>0) {
                    
                } else {
                    //print_object($pdata); exit;
                    $save_result = $this->Users->save_user($pdata, $this->input->post("userid",true));
                    if($save_result['result'] == 'S') {
                        $isupdated = true;
                        $data["successes"] = array('User added successfully.');
                        $data["postdata"] = "";
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
                
            } 
            
            if( $isupdated || !$ispostdata ) {
                $mi_result = $this->Users->get_user_list(0,0,array('userid'=> $this->input->post("userid",true)));
                //print_object($mi_result);exit;
                if(count($mi_result) == 0) {
                    $data['errors'] = array("Could not find the selected User");
                } else {
                    $pdata = array();
                    $data['userid'] = $mi_result[0]['userid'];
                    $pdata['username'] = $mi_result[0]['username'];
                    $pdata['roleid'] = $mi_result[0]['roleid'];
                    $pdata['roletitle'] = $mi_result[0]['roletitle'];
                    $pdata['disabled'] = $mi_result[0]['disabled'];
                    $pdata['name'] = $mi_result[0]['name'];
                    $pdata['mobile'] = $mi_result[0]['mobile'];
                    $pdata['email'] = $mi_result[0]['email'];
                    $pdata['address'] = $mi_result[0]['address'];
                    //$data['firstlogin'] = $mi_result[0]['firstlogin'];
                    $pdata['createdby'] = $mi_result[0]['createdby'];
                    $pdata['createdon'] = $mi_result[0]['createdon'];
                    $data['editaction'] = 'admin/users/edituser';
                    $data['postdata'] = $pdata;
                }
            }
            
            $navResult = $this->Dataset_db->buildNav();
            $data['rolelist'] = $this->Users->get_roles();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //$data['dgdata'] = $this->Menu->build_mg_page_data();
            //print_object($pdata); exit;
            $data['content'] = $this->load->view('edituser',$data,true);
            $this->load->vars($data);
            $this->load->view('admin/default_view'); 
       }
       
       function deleteuser() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            //print_object($_POST);exit;
            $data['ptitle'] = "Delete User";
            if( isset( $_POST["userid"] ) ) {
                $mg_result = $this->Users->delete_user(array('userid'=> $this->input->post("userid",true)));
                //print_object($mg_result);exit;
                if(!$mg_result) {
                    $data["errors"] = array('Could not find the selected User.');
                } else {
                    $data["successes"] = array('User deleted successfully.');
                }
            }
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $data['dgdata'] = $this->Users->build_users_dg_data(); 
            //print_object($data); exit;
            $data['content'] = $this->load->view('viewuser',$data,true);
            $this->load->vars($data);
            $this->load->view('admin/default_view'); 
       }
       
       function changepswd() {
            $user = $this->session->userdata('user');
            $data["userid"] = $user['userid'];
            $data['ptitle'] = "Add User";
            //print_object($_POST); exit;
            if($_POST['btnChangePassword']) {
                //print_object($_POST); exit;
                $this->form_validation->set_rules('oldpassword', 'Old Password', 'trim|required|max_length[20]');
                $this->form_validation->set_rules('password', 'New Password', 'trim|required|min_length[6]|max_length[20]|matches[cpassword]');
                $this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|min_length[6]|max_length[20]');
                $this->form_validation->run();
                $data["errors"] = $this->form_validation->error_array();
                //print_object($_POST); exit;
                $oldpswd = $this->input->post("oldpassword",true);
                $newpswd = $this->input->post("password",true);
                
                if(!$this->Users->is_old_password($user['userid'], $oldpswd)) {
                    if(is_array($data['errors']))
                        array_push($data["errors"],"Wrong Current Password.");
                    else
                        $data["errors"] = array("Wrong Current Password.");
                }
                
                if($oldpswd==$newpswd) {
                    if(is_array($data['errors']))
                        array_push($data["errors"],"Current and New Password are same.");
                    else
                        $data["errors"] = array("Current and New Password are same.");
                }
                   
                if (count($data["errors"])>0) {
                    
                } else {
                    $pdata = array();
                    $pdata["password"] = md5($newpswd);
                    
                    $save_result = $this->Users->save_user($pdata, $user['userid']);
                    if($save_result['result'] == 'S') {
                        $data["successes"] = array('Password changed successfully.');
                        $data["postdata"] = "";
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
            }
                        
            $navResult = $this->Dataset_db->buildNav();
            $data['rolelist'] = $this->Users->get_roles();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $data['content'] = $this->load->view('admin/changepassword',$data,true);
            $this->load->vars($data);
            $this->load->view('admin/default_view');
       }
       
       
       
	}

/* End of file */